﻿using System;
using System.Diagnostics;
using System.Drawing.Imaging;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Reflection;
using System.Text;
using System.Windows.Forms;
using mirabyte.Mobile.TouchControls2;
using SartmenuEditor.helper;
using SartmenuEditor.Properties;

namespace SartmenuEditor
{
    public partial class frmMain : frmBase
    {
        private readonly string PATH = Environment.GetFolderPath(Environment.SpecialFolder.StartMenu);
        private List<MenuEntry> MenuEntries;
        private MenuEntry parent;

        public frmMain()
        {
            InitializeComponent();
            setLayout();
            loadFolder();
        }

        private void setLayout()
        {
            mnuExit.Text = Global.getString("general_close");
            mnuOptions.Text = Global.getString("general_options");
            mnuAbout.Text = Global.getString("btn_about");
            tlistFolder.ItemClick += new TouchListBox.OnItemClick(tlistFolder_ItemClick);

        }

        void tlistFolder_ItemClick(object sender, ItemEventArgs e)
        {
            if (((MenuEntry)e.Item.Tag).Folder != PATH)
            {
                parent = new MenuEntry();
                parent.Folder = new DirectoryInfo(((MenuEntry)e.Item.Tag).Folder).Parent.FullName;
                parent.Name = Global.getString("general_back");
                parent.Type = EntryType.Parent;
            }
            else
            {
                parent = null;
            }
            switch (((MenuEntry)e.Item.Tag).Type)
            {
                case EntryType.Folder:
                    Debug.WriteLine("Folder");
                    loadFolder(((MenuEntry)e.Item.Tag).Folder, parent);
                    break;
                case EntryType.File:
                    Debug.WriteLine("File");
                    FileInfo((MenuEntry)e.Item.Tag);
                    break;
                case EntryType.Parent:
                    loadFolder(((MenuEntry)e.Item.Tag).Folder, parent);
                    break;
                default:
                    Debug.WriteLine("None");
                    break;
            }
        }

        private void FileInfo(MenuEntry file)
        {
            frmEdit _frmEdit = new frmEdit();
            _frmEdit.mnuEntry = file;
            _frmEdit.Closed += new EventHandler(_frmEdit_Closed);
            _frmEdit.Show();
        }

        void _frmEdit_Closed(object sender, EventArgs e)
        {
            frmEdit closedForm = (frmEdit) sender;
            string from = closedForm.mnuEntry.Folder;
            string to = string.Format(@"{0}\{1}", closedForm.cbFolders.Text, closedForm.mnuEntry.Name);
            if (string.IsNullOrEmpty(closedForm.cbFolders.Text)) return;
            moveFile(from, to);
            loadFolder();
        }

        private void moveFile(string from, string to)
        {
            try
            {
                File.Move(from, to);
            }
            catch(IOException ex)
            {
                
            }
            catch(Exception ex)
            {
                
            }
        }

        private void loadFolder()
        {
            loadFolder(PATH, null);
        }

        private void loadFolder(string path, MenuEntry parent)
        {
            try
            {
                Cursor.Current = Cursors.WaitCursor;
                if (MenuEntries == null)
                {
                    MenuEntries = new List<MenuEntry>();
                }
                else
                {
                    MenuEntries.Clear();
                }
                DirectoryInfo info = new DirectoryInfo(path);
                setTitle(info.Name);
                FileInfo[] files = info.GetFiles();
                DirectoryInfo[] folders = info.GetDirectories();
                if (files.Length == 0)
                {
                    Debug.WriteLine("Keine Dateien gefunden");
                    Cursor.Current = Cursors.Default;
                    Global.showMessageBox("No Files", "NO files found", Global.getString("general_ok"));
                    loadFolder(parent.Folder,null);
                    return;
                }

                foreach (DirectoryInfo folder in folders)
                {
                    MenuEntries.Add(new MenuEntry()
                                        {
                                            Folder = folder.FullName,
                                            Name = folder.Name,
                                            Type = EntryType.Folder
                                        });
                }

                foreach (FileInfo file in files)
                {
                    MenuEntries.Add(new MenuEntry()
                                        {
                                            Folder = file.FullName,
                                            Name = file.Name,
                                            Type = EntryType.File
                                        });
                }
                if (parent != null)
                {
                    MenuEntries.Add(parent);
                }


                updateList();
            }
            catch (DirectoryNotFoundException ex)
            {
                Debug.WriteLine("Directory not found!");
            }
            catch (Exception ex)
            {

            }
            finally
            {
                Cursor.Current = Cursors.Default;
            }
        }

        private void updateList()
        {
            tlistFolder.Items.Clear();


            foreach (MenuEntry menuEntry in MenuEntries)
            {
                TouchListDefaultItem item = new TouchListDefaultItem(tlistFolder);
                if(menuEntry.Type == EntryType.Folder)
                {
                    //item.LeftIcon = Global.getAlphaImage("Folder_Closed",new Size(50,50));
                    item.Height = 60;
                    item.LeftIconPosition.TopSpace = 5;
                    item.LeftIconPosition.BottomSpace = 5;
                    item.LeftIconPosition.LeftSpace = 5;
                    item.LeftIconPosition.RightSpace = 5;
                    item.TextPosition.LeftSpace = 60;
                    item.ShowLeftIcon = true;
                }
                else
                {
                    //item.LeftIcon = Global.getAlphaImage("Aqua_Ball_Green",new Size(50,50));
                    item.Height = 60;
                    item.LeftIconPosition.TopSpace = 5;
                    item.LeftIconPosition.BottomSpace = 5;
                    item.LeftIconPosition.LeftSpace = 5;
                    item.LeftIconPosition.RightSpace = 5;
                    item.TextPosition.LeftSpace = 60;
                    item.ShowLeftIcon = true;   
                }
                item.Text = menuEntry.Name;
                item.Tag = menuEntry;
                tlistFolder.Items.Add(item);
            }


            tlistFolder.UpdateList(AnimationDirection.adLeft);
        }

        private void mnuExit_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void mnuAbout_Click(object sender, EventArgs e)
        {
            frmAbout _frmAbout = new frmAbout();
            _frmAbout.Show();
        }
    }

    public class MenuEntry
    {
        private Image _icon;
        private string _name;
        private string _folder;
        private EntryType _type;

        public string Name
        {
            get { return _name; }
            set { _name = value; }
        }

        public string Folder
        {
            get { return _folder; }
            set { _folder = value; }
        }

        public EntryType Type
        {
            get { return _type; }
            set { _type = value; }
        }

        public Image Icon
        {
            get { return _icon; }
            set { _icon = value; }
        }
    }

    public enum EntryType
    {
        Parent,
        Folder,
        File,
        None
    }
}